package bi.i99click;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.jdom.Attribute;
import org.jdom.Comment;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.xml.sax.InputSource;

@SuppressWarnings("unused")
public class BaseClick {
	Map<String, String> datas = new HashMap<String, String>();

	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected void get99ClickApi(String method, String date) {
		Object[] ob = null;
		try {
			XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
			config.setServerURL(new URL("https://api.99click.com/api/c/sfrpc"));
			XmlRpcClient client = new XmlRpcClient();
			client.setConfig(config);
			int memberid = 1273;// 会员id
			int compid = 2018; // 公司id
			String userName = "T2x8HLa";// 用户名
			String passWord = "W*HT!UAS_";// 用户密码
			HashMap hashmap = new HashMap();
			hashmap.put("memberid", new Integer(memberid));// 会员id
			hashmap.put("compid", new Integer(compid));// 公司id
			hashmap.put("username", userName);// 用户名
			hashmap.put("password", passWord);// 密码
			hashmap.put("date", date);// 日期，格式形如2010-01-01
			Vector params = new Vector();
			params.addElement(hashmap);
			String result = (String) client.execute(method,
					params);
			String xmlStr = result;
			StringReader sr = new StringReader(xmlStr);
			InputSource is = new InputSource(sr);
			Document doc = (new SAXBuilder()).build(is); //
			Element root = doc.getRootElement();
			process(root);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void process(Element element) {
		inspect(element);
		List content = element.getContent();// 取元素的所有内容
		Iterator iterator = content.iterator();
		while (iterator.hasNext()) {
			Object o = iterator.next();
			if (o instanceof Element) {// 如果是子元素
				Element child = (Element) o;
				process(child);// 递归调用
			}
		}
	}

	private void inspect(Element element) {
		String qualifiedName = element.getQualifiedName();
		datas.put(qualifiedName, element.getText());
	}

}
